package code.structure.manager.pop.view 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	import code.structure.manager.sound.MySoundManager;
	import code.tools.behavior.IAppear;
	import code.tools.behavior.IDisappear;
	import code.tools.method.CommonMethod;
	import code.tools.sound.src.SoundName;
	
	/**
	 * 面板的父类 进行一些主要公共方法的封装
	 * @author CYang
	 */
	public class Panel extends Sprite implements IAppear, IDisappear 
	{
		protected var _sMain:Sprite;
		/**
		 * 面板
		 */
		protected var _mMain:Sprite;
		/**
		 * 资源
		 */
		protected var _mLibs:Sprite;
		/**
		 * 出现
		 */
		protected var _IAppear:IAppear;
		/**
		 * 消失
		 */
		protected var _IDisappear:IDisappear;
		/**
		 * 面板是否在显示中
		 */
		public var bVisible:Boolean;
		/**
		 * 类名
		 */
		protected var _sClassName:String;
		private var _isBuilded:Boolean;
		private var _freezeBmp:Bitmap;
		/**
		 * 构造函数
		 */
		public function Panel() 
		{
			_sMain = new Sprite();
			addChild(_sMain);
			_mMain = new Sprite();
			tabChildren = false;
			tabEnabled = false;
			_sMain.addChild(_mMain);
			
			addEventListener(Event.ADDED_TO_STAGE, addToStage);
		}
		/**
		 * 获取主面板资源
		 * @param	e
		 */
		private function addToStage(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addToStage);
		
			initializePanel();
			_isBuilded = true;
		}
		/**
		 * 手动构建面板 移动
		 */		
		public function buildPanel():void
		{
			if(_isBuilded)return;
			addToStage(null);
		}
		/**
		 * 各子面板初始化
		 */
		protected function initializePanel():void 
		{
			//override buy sub class……
		}
		/**
		 * 出现
		 */
		public function appear():void
		{
			bVisible = true;
			
			_mMain.x = -this.width*.5;
			_mMain.y = -this.height*.5;
			_sMain.x = this.width*.5;
			_sMain.y = this.height*.5;
			_IAppear.appear();
		}
		
		/**
		 * 消失
		 */
		public function disappear():void
		{
			MySoundManager.playSound(SoundName.CLOSE_WINDOW, 0.5);
			//ProjectAssistManager.getIntance().popManager.isClosing = true;
			_mMain.x = -this.width*.5;
			_mMain.y = -this.height*.5;
			_sMain.x = this.width*.5;
			_sMain.y = this.height*.5;
			_IDisappear.disappear();
		}
		/**
		 * 销毁方法
		 */
		public function dispose():void
		{
			//override buy sub class
		}
		/**
		 * 设置面板坐标
		 * called by sub class
		 * @param	_posx
		 * @param	_posy
		 */
		protected function setMainPanelPosition(_posx:Number = 0, _posy:Number = 0):void
		{
			if (_mMain)
			{
				var _matrix:Matrix = _mMain.transform.matrix;
				_matrix.translate(_posx, _posy);
				_mMain.transform.matrix = _matrix;
			}
			else trace("\t[Error] : _mMain is null cannot set position\n");
		}
		
		/**
		 * 对面板进行定格  
		 * 把面板当前状态拍成位图显示，隐藏实际面板
		 */		
		public function freeze():void
		{
			var rect:Rectangle = _mMain.getBounds(_mMain);
			var bmd:BitmapData = CommonMethod.drawBitmapData(_mMain);
			if(_freezeBmp == null)
				_freezeBmp = new Bitmap();
			_freezeBmp.bitmapData = bmd;
			_freezeBmp.x = rect.x + _mMain.x;
			_freezeBmp.y = rect.y + _mMain.y;
			//
			_sMain.addChild(_freezeBmp);
			_mMain.visible = false;
		}
		
		/**
		 * 接触定格 
		 */		
		public function unfreeze():void
		{
			if(_freezeBmp.bitmapData !=null)
				_freezeBmp.bitmapData.dispose();
			_freezeBmp.parent && _freezeBmp.parent.removeChild(_freezeBmp);
			_mMain.visible = true;
		}
	}
	
}